package com.xiaozhen.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaozhen.blog.entity.Blog;
import com.xiaozhen.blog.entity.Comment;
import com.xiaozhen.blog.entity.Event;
import com.xiaozhen.blog.entity.User;
import com.xiaozhen.blog.service.BlogService;
import com.xiaozhen.blog.service.CommentService;
import com.xiaozhen.blog.service.EventService;
import com.xiaozhen.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * @create 2023-05-24 11:48
 * 消息
 */
@Controller
@RequestMapping("/message")
public class messageController {

    @Autowired
    private EventService eventService;

    /**
     * 获取通知消息列表
     * @param messageType
     * @param model
     * @return
     */
    @GetMapping("/{messageType}")
    public String message(@PathVariable int messageType, Model model) {

        List<Event> events = eventService.getEventsByMessageType(messageType);

        model.addAttribute("events", events);
        model.addAttribute("messageType", messageType);

        return "message";
    }

    /**
     * 清空评论通知消息
     *
     * @param messageType
     * @return
     */
    @GetMapping("/delete/{messageType}")
    public String deleteAllMessage(@PathVariable int messageType) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userId = authentication.getName();
        LambdaQueryWrapper<Event> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Event::getUserId, userId);
        wrapper.eq(Event::getEntityType, messageType);
        eventService.remove(wrapper);
        return "redirect:/message/" + messageType;
    }

    /**
     * 通过 eventId 删除评论通知消息
     *
     * @param eventType
     * @param eventId
     * @return
     */
    @GetMapping("/delete/{eventType}/{eventId}")
    public String deleteMessageByEventId(@PathVariable long eventType, @PathVariable int eventId) {

        eventService.removeById(eventId);

        return "redirect:/message/" + eventType;
    }
}
